import {createRouter, createWebHashHistory, RouteRecordName, RouteRecordRaw} from "vue-router";
import {App} from "vue";
import {BasicsRoutes, PAGE_NOT_FOUND_ROUTE} from "/@/router/basis";
import {portalRoutes} from "/@/router/portal";
import {ConsoleRoutes} from "/@/router/console";


export const routes: RouteRecordRaw[] = [
    ...BasicsRoutes,
    ...portalRoutes,
    ...ConsoleRoutes,
    PAGE_NOT_FOUND_ROUTE
]

// 获取清除路由时不应该清除基础路由
// 白名单应该包含基本静态路由
const WHITE_NAME_LIST: string[] = [];
const getRouteNames = (array: any[]) =>
    array.forEach((item) => {
        WHITE_NAME_LIST.push(item.name);
        getRouteNames(item.children || []);
    });
getRouteNames(routes);


// reset router
export function resetRouter() {
    router.getRoutes().forEach((route) => {
        const {name} = route;
        if (name && !WHITE_NAME_LIST.includes(name as string)) {
            router.hasRoute(name) && router.removeRoute(name);
        }
    });
}


export const router = createRouter({
    history: createWebHashHistory(),
    routes
})


export function setupRouter(app: App<Element>) {
    app.use(router);
}

